package com.launch.wlfw.controller;

import com.github.pagehelper.PageInfo;
import com.launch.wlfw.common.Result;
import com.launch.wlfw.dto.LendListDTO;
import com.launch.wlfw.pojo.BookInfo;
import com.launch.wlfw.pojo.ReaderInfo;
import com.launch.wlfw.service.LendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Controller
public class LendListController {

    @Resource
    private LendService lendService;



    @ExceptionHandler(Exception.class)
    public Result doException(Exception ex){
        return Result.error();
    }

    @RequestMapping("/lendListIndex")
    public String lendListIndex() {
        return "lend/lendListIndex";
    }

    @RequestMapping("/addLendList")
    public String addLendList() {
        return "lend/addLendList";
    }

    @RequestMapping("/excBackBook")
    public ModelAndView excBackBook(Integer id, Integer bookId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("lend/excBackBook");
        modelAndView.addObject("id",id);
        modelAndView.addObject("bid",bookId);
        return modelAndView;
    }


    @RequestMapping("/lendListAll")
    @ResponseBody
    public Result<PageInfo<LendListDTO>> selectAll(@RequestParam Map<String,String> param) {
        log.info("parameter:"+param.toString());
        PageInfo<LendListDTO> lendPageInfo = lendService.selectByParams(param);
        return Result.success(lendPageInfo);
    }

    @RequestMapping("/deleteLendListByIds")
    @ResponseBody
    public Result<Integer> deleteLendListByIds(@RequestParam List<Integer> ids, @RequestParam List<Integer> bookIds) {
        int i = lendService.deleteById(ids, bookIds);
        if (i != 0) {
            return Result.success();
        } else {
            return Result.error();
        }
    }

    @RequestMapping("/backLendListByIds")
    @ResponseBody
    public Result<Integer> backLendListByIds(@RequestParam List<Integer> ids, @RequestParam List<Integer> bookIds) {
        int i = lendService.backLendListByIds(ids, bookIds);
        if (i != 0) {
            return Result.success(i);
        } else {
            return Result.error();
        }
    }

    @RequestMapping("/bookInfoAll")
    @ResponseBody
    public Result<PageInfo<BookInfo>> addLendList(@RequestParam Map<String, String> param) {
        PageInfo<BookInfo> lendPageInfo = lendService.getAllBook(param);
        return Result.success(lendPageInfo);
    }


    @RequestMapping("/addLend")
    @ResponseBody
    public Result<Integer> addLend(String readerNumber, @RequestParam List<Integer> ids) {
        Integer i = lendService.addLend(readerNumber, ids);
        if (i != 0) {
            return Result.success();
        } else {
            return Result.error("2005", "没有该借书卡号！");
        }
    }


    @RequestMapping("/updateLendInfoSubmit")
    @ResponseBody
    public Result<Integer> updateLendInfoSubmit(@RequestParam Map<String,String> params) {
        int i = lendService.updateLendInfoSubmit(params);
        return Result.success();
    }


    @RequestMapping("/queryLookBookList")
    @ResponseBody
    public ModelAndView queryLookBookList(@RequestParam Map<String,String> params) {
        ModelAndView modelAndView = new ModelAndView();
        List<LendListDTO> lendListDTOS = lendService.queryLookBookList(params);
        modelAndView.setViewName("lend/lookBookList");
        modelAndView.addObject("info",lendListDTOS);
        return modelAndView;
    }


    @RequestMapping("/queryLookbookInfoList2")
    public String lendListIndex2(HttpServletRequest request, Model model) {
        Object object = request.getSession().getAttribute("user");
        ReaderInfo readerInfo = (ReaderInfo) object;
        // 获取借书信息
        Map<String, String> param = new HashMap<>();
        param.put("readerId", readerInfo.getId().toString());
        log.info("查询借书信息："+readerInfo.getId());
        log.debug("当前借书信息"+param.get("readerId"));
        List<LendListDTO> lendListDTOS = lendService.queryLookBookList(param);
        model.addAttribute("info", lendListDTOS);
        return "lend/lookBookList";
    }

}
